package cn.com.duiba.millionaire.center.biz.service.impl;

import cn.com.duiba.millionaire.center.api.constant.MillWebRedisKeyFactory;
import cn.com.duiba.millionaire.center.api.dto.ActQuestionIndexDataDto;
import cn.com.duiba.millionaire.center.api.dto.ActStatisticalDataDto;
import cn.com.duiba.millionaire.center.api.dto.MillionaireActivityDto;
import cn.com.duiba.millionaire.center.biz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by sunchangji on 2018/3/17.
 */
@Service
public class ActStatisticalDataServiceImpl implements ActStatisticalDataService {
    @Autowired
    private MillionaireActivityService millionaireActivityService;
    @Autowired
    private QuestionSelectionStastisticService questionSelectionStastisticService;
    @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate stringRedisTemplate;//在线人数
    @Autowired
    private RenascenceUsageInfoCacheService renascenceUsageInfoCacheService;//每一题使用复活卡人数
    @Autowired
    private WinnerListService winnerListService;//答对人数统计

    @Override
    public ActStatisticalDataDto getStatisticalDataByActId(Long activityId) {
        MillionaireActivityDto activityDto = millionaireActivityService.findMillionaireActivityById(activityId);
        if (activityDto == null) {
            return null;
        }
        Long totalMoney = activityDto.getTotalMoney();
        Long questionNum = activityDto.getQuestionNum();
        Long winnerNum = winnerNum(activityId);
        Long joinNum = getOnlineNum(activityId);
        ActStatisticalDataDto dataDto = new ActStatisticalDataDto();
        dataDto.setActivityId(activityId);
        dataDto.setActType(1);
        dataDto.setJoinNum(joinNum);
        dataDto.setWinnerNum(winnerNum);
        dataDto.setTotalMoney(String.valueOf(totalMoney/100));
        dataDto.setWinnerMoney(getEveryConsumerWinMoney(totalMoney, winnerNum));
        dataDto.setIndexData(questionIndexDatas(activityId, questionNum));
        return dataDto;
    }

    /**
     * 获取每一题的答题情况数据
     *
     * @param activityId  活动id
     * @param questionNum 总题目数
     * @return
     */
    private List<ActQuestionIndexDataDto> questionIndexDatas(Long activityId, Long questionNum) {
        Integer questNums = questionNum.intValue();
        List<ActQuestionIndexDataDto> indexDataDtos = new ArrayList<>();
        //每一题使用复活卡情况数据
        List<Long> renascenceNums = getRenascenceNum(activityId, questNums);
        for (int i = 1; i <= questNums; i++) {
            ActQuestionIndexDataDto dataDto = new ActQuestionIndexDataDto();
            dataDto.setIndex(i);
            dataDto.setReviveNum(renascenceNums.get(i - 1));
            dataDto.setSelectNum(getSelectNum(activityId, i));
            indexDataDtos.add(dataDto);
        }

        return indexDataDtos;
    }

    /**
     * 获取每一个选项的选择人数
     *
     * @param activityId
     * @param index
     * @return
     */
    private List<Long> getSelectNum(Long activityId, Integer index) {
        Map<String, Long> everyAnswerSelectedMap = questionSelectionStastisticService.findQuestionStastistic(activityId, index);
        List<Long> selectedList = new ArrayList<>();
        if (everyAnswerSelectedMap == null || everyAnswerSelectedMap.isEmpty()) {
            for (int i = 0; i < 3; i++) {
                selectedList.add(0L);
            }
            return selectedList;
        }
        Long aOption = everyAnswerSelectedMap.get("A");
        Long bOption = everyAnswerSelectedMap.get("B");
        Long cOption = everyAnswerSelectedMap.get("C");
        selectedList.add(aOption == null ? 0L : aOption);
        selectedList.add(bOption == null ? 0L : bOption);
        selectedList.add(cOption == null ? 0L : cOption);

        return selectedList;
    }

    /**
     * 获取每一题使用复活卡人数
     *
     * @param activityId  活动id
     * @param questionNum 总共题目数
     * @return
     */
    private List<Long> getRenascenceNum(Long activityId, Integer questionNum) {
        List<Long> renascenceCards = new ArrayList<>(questionNum);
        for (int i = 1; i <= questionNum; i++) {
            Long renascenceNum = renascenceUsageInfoCacheService.findCount(activityId, i);
            renascenceCards.add(renascenceNum);
        }
        return renascenceCards;
    }

    /**
     * 获取奖金总人数
     *
     * @param activityId
     * @return
     */
    private Long winnerNum(Long activityId) {
        return winnerListService.countWinners(activityId);
    }

    /**
     * 获取用户瓜分钱数
     *
     * @param totalMoney 实际总金额扩大100倍的
     * @param winnerNum  全部正确的人数
     * @return 扩大100倍的金额
     */
    private String getEveryConsumerWinMoney(Long totalMoney, Long winnerNum) {
        if (winnerNum == 0) {
            return "0";
        }

        Long winMoney = totalMoney / winnerNum;
        if (winMoney <= 10) {
            return "0";
        }
        return BigDecimal.valueOf(winMoney).movePointLeft(2).toPlainString();
    }

    /**
     * 获取参与人数
     *
     * @param activityId
     * @return
     */
    @Override
    public Long getOnlineNum(Long activityId) {
        return stringRedisTemplate.opsForValue().increment(MillWebRedisKeyFactory.K004.toString() + activityId, 0);
    }
}
